package club.hicode.daydayup.threadpool;

import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;
import java.util.concurrent.TimeUnit;

/**
 * 终端测试
 *
 * @author Liu Chunfu
 * @date 2018-06-26 上午9:34
 **/
public class Interrupting {
    private static ExecutorService exec = Executors.newCachedThreadPool();

    /**
     * 统一设置中断方法
     *
     * @param r
     * @throws InterruptedException
     */
    public static void test(Runnable r) throws InterruptedException {
        Future<?> f = exec.submit(r);
        TimeUnit.MICROSECONDS.sleep(100);
        System.out.println("Inteerupt " + r.getClass().getName());
        f.cancel(true);//中断标识设置为中断
        System.out.println("Interrupt sent to " + r.getClass().getName());
    }

    public static void main(String[] args) throws Exception {
        //test(new SleepBlocked());
        //test(new IOBlocked(System.in));
        test(new SynchronizedBlocked());
        TimeUnit.SECONDS.sleep(3);
        System.out.println("Aborting with System(0)");
        System.exit(0);
    }
}